package com.zjw.zy.service.net;

import androidx.annotation.NonNull;

import com.zjw.zy.bean.UIMode;
import com.zjw.zy.bean.User;
import com.zjw.zy.bean.Weather15Day;
import com.zjw.zy.bean.Weather1Day;
import com.zjw.zy.constant.HttpConstant;
import com.zjw.zy.constant.RegisterUserResult;
import com.zjw.zy.constant.WeatherConstant;
import com.zjw.zy.service.contract.IHeartService;
import com.zjw.zy.service.contract.IUserService;
import com.zjw.zy.service.contract.IWeatherService;
import com.zjw.zy.service.net.factory.ConvertFactory;
import com.zjw.zy.smi.JustWithIt;
import com.zjw.zy.utils.GsonUtils;
import com.zjw.zy.utils.LogUtils;
import com.zjw.zy.utils.SharePrefUtils;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 16:03
 */
public final class HttpService {

    private static final String TAG = "HttpUtils";

    private static final IWeatherService weatherService = applyRetrofit().create(IWeatherService.class);

    private static final IHeartService heartService = applyRetrofit().create(IHeartService.class);

    private static final IUserService userService = applyRetrofit().create(IUserService.class);


    private static Retrofit retrofit;

    private static Retrofit applyRetrofit() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder().baseUrl(HttpConstant.HEART_SERVICE_BASE_URL)
                    .addConverterFactory(new ConvertFactory()).build();
        }
        return retrofit;
    }

    //请求天气
    public static void request15Weather(@NonNull JustWithIt<Weather15Day> observer) {

        Call<Weather15Day> weatherCall = weatherService.getWeather(15, SharePrefUtils.getInt(WeatherConstant.WEATHER_CURRENT_CITY_AREA_CODE_KEY, 101040100));

        weatherCall.enqueue(new Callback<Weather15Day>() {
            @Override
            public void onResponse(@NonNull Call<Weather15Day> call, @NonNull Response<Weather15Day> response) {
                observer.onWith(response.body());
            }

            @Override
            public void onFailure(@NonNull Call<Weather15Day> call, @NonNull Throwable t) {
                LogUtils.e(TAG, "requestWeather onFailure");
                observer.onWith(null);
            }

        });
    }


    public static void requestAppUiMode(@NonNull JustWithIt<UIMode> observer) {

        heartService.getUIMode().enqueue(new Callback<UIMode>() {
            @Override
            public void onResponse(@NonNull Call<UIMode> call, @NonNull Response<UIMode> response) {
                observer.onWith(response.body());
            }

            @Override
            public void onFailure(@NonNull Call<UIMode> call, @NonNull Throwable t) {
                LogUtils.e(TAG, "requestAppUiMode onFailure");
                observer.onWith(UIMode.defaultMode());
            }
        });
    }


    //请求天气
    public static void requestHourlyWeather(@NonNull JustWithIt<Weather1Day> observer) {

        //todo 101040100 重庆
        Call<Weather1Day> weatherCall = weatherService.getHourWeather(24, SharePrefUtils.getInt(WeatherConstant.WEATHER_CURRENT_CITY_AREA_CODE_KEY, 101040100));

        weatherCall.enqueue(new Callback<Weather1Day>() {
            @Override
            public void onResponse(@NonNull Call<Weather1Day> call, @NonNull Response<Weather1Day> response) {
                observer.onWith(response.body());
            }

            @Override
            public void onFailure(@NonNull Call<Weather1Day> call, @NonNull Throwable t) {
                LogUtils.e(TAG, "requestWeather onFailure");
                observer.onWith(null);
            }

        });
    }


    public static void requestRegister(User user, @RegisterUserResult.Des @NonNull JustWithIt<Integer> observer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), GsonUtils.toJson(user));
        Call<Integer> call = userService.register(requestBody);

        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(@NonNull Call<Integer> call, @NonNull Response<Integer> response) {
                observer.onWith(response.body());
            }

            @Override
            public void onFailure(@NonNull Call<Integer> call, @NonNull Throwable t) {
                LogUtils.e(TAG, t.getLocalizedMessage());
                observer.onWith(RegisterUserResult.FAILED);
            }
        });
    }

    public static void requestLover(User user, JustWithIt<User> observer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), GsonUtils.toJson(user));

        Call<User> call = userService.lover(requestBody);

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(@NonNull Call<User> call, @NonNull Response<User> response) {
                observer.onWith(response.body());
            }

            @Override
            public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
                LogUtils.e(TAG, t.getLocalizedMessage());
                observer.onWith(null);
            }
        });

    }
}
